using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HealthUI : MonoBehaviour {

    public GameObject healthIcon;
    public GameObject healthIconBlack;
    public float offset;
    public GameObject player;

    int heartsDisplayed;
    List<GameObject> hearts;

	// Use this for initialization
	void Start () {
        heartsDisplayed = 0;
        hearts = new List<GameObject>();
	}
	
	// Update is called once per frame
	void Update () {
        int heartsToDisplay = player.GetComponent<Damagable>().currentHP;
        if (heartsToDisplay > 10) heartsToDisplay = 10;
        if (heartsDisplayed != heartsToDisplay)
        {
            foreach (GameObject heart in hearts)
            {
                Destroy(heart);
            }
            hearts.Clear();
            for (int i = 0; i < heartsToDisplay; i++)
            {
                GameObject heart;
                if (GlobalState.isRedHero)
                    heart = Instantiate(healthIcon) as GameObject;
                else
                    heart = Instantiate(healthIconBlack) as GameObject;
                heart.transform.parent = gameObject.transform;
                Vector3 localPos = new Vector3(i * offset, 0.0f, 0.0f);
                heart.transform.localPosition = localPos;
                hearts.Add(heart);
            }
            heartsDisplayed = heartsToDisplay;
        }
	}
}
